//
//  CustomerDetailTableViewController.m
//  CollectionManagerFlow
//
//  Created by Sophia Tsay on 5/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "CustomerDetailTableViewController.h"

@interface CustomerDetailTableViewController ()
@property (strong, nonatomic) IBOutlet UITableView *detailTable;
@property (strong, nonatomic) NSString *collectorName;
@property (nonatomic, strong) CollectorsListTableViewController *collectorsPopoverVC;
@end

@implementation CustomerDetailTableViewController
@synthesize detailTable = _detailTable;
@synthesize collectorName = _collectorName;
@synthesize collectorsPopoverVC = _collectorsPopoverVC;
@synthesize objectType = _objectType;

#define DEFAULT_COLLECTOR_NAME @"Claudia Renner";
#define COLLECTOR_DEFAULT_NAME @"collector_default_name"

- (NSString *)collectorName
{
    if (!_collectorName) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *default_collectorName = [defaults objectForKey:COLLECTOR_DEFAULT_NAME];
        if (!default_collectorName) {
            return DEFAULT_COLLECTOR_NAME;
        } else {
            return default_collectorName;
        }
    } else {
        return _collectorName;
    }    
}

- (void) setCollectorName:(NSString *)collectorName
{
    if (collectorName != _collectorName) {
        _collectorName = collectorName;
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:_collectorName forKey:COLLECTOR_DEFAULT_NAME];
        [defaults synchronize];
    }
}

- (void) setDetailTable:(UITableView *)detailTable
{
    _detailTable = detailTable;
    self.detailTable.backgroundView = nil;
    self.detailTable.backgroundColor = [UIColor blackColor];
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (!self.objectType) {
        self.objectType = @"Customer";
    }
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [self setDetailTable:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return YES;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Reassign Collector"])
    {
        self.collectorsPopoverVC = (CollectorsListTableViewController *)segue.destinationViewController;
        self.collectorsPopoverVC.popoverDeleage = self;
        self.modalPresentationStyle = UIModalPresentationFormSheet;
        [self.collectorsPopoverVC removeDuplicateCollector:self.collectorName];
    }
}

- (void)dismissAtTap
{
    [self.detailTable reloadData];
    [[self.view.subviews lastObject] removeFromSuperview];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"Object type is %@", self.objectType);
    if ([self.objectType isEqualToString:@"Customer"]) {
        return 2;
    } else {
        return 1;        
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numberOfRows = 0;
    if (section == 0) {
        if ([self.objectType isEqualToString:@"Customer"]) {
            numberOfRows = [detailTitleList count];
        } else if ([self.objectType isEqualToString:@"Collector"]) {
            numberOfRows = [collectorDetailTitles count];
        }
    } else {
        numberOfRows = 1;
    }
    return numberOfRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 
    static NSString *DetailCellIdentifier = @"Customer Detail Cell";
    static NSString *CollectorCellIdentifier = @"Collector Cell";

    UITableViewCell *cell;
    
    if (indexPath.section == 0) {  

        cell = [tableView dequeueReusableCellWithIdentifier:DetailCellIdentifier];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:DetailCellIdentifier];
        }
        
        if ([self.objectType isEqualToString:@"Customer"]) {
            cell.textLabel.text = [detailTitleList objectAtIndex:indexPath.row];
            cell.detailTextLabel.text = [detailValueList objectAtIndex:indexPath.row];     
        } else if ([self.objectType isEqualToString:@"Collector"]) {
            cell.textLabel.text = [collectorDetailTitles objectAtIndex:indexPath.row];
            cell.detailTextLabel.text = [collectorDetailValues objectAtIndex:indexPath.row];                 
        }
    } else {

        cell = [tableView dequeueReusableCellWithIdentifier:CollectorCellIdentifier];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CollectorCellIdentifier];
        }

        cell.textLabel.text = self.collectorName;
        cell.detailTextLabel.text = @"CEI: 87.21";
    }
    cell.backgroundColor = [UIColor darkGrayColor];
    cell.textLabel.textColor = [UIColor lightGrayColor];
    cell.detailTextLabel.textColor = [UIColor whiteColor];
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.textLabel.numberOfLines = 2;
    [cell.textLabel sizeToFit];
        
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return @"Detail Information";
    } else {
        return @"Assigned Collector";
    }
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 8, tableView.bounds.size.width -16, 16)];
    headerLabel.textColor = [[UIColor alloc] initWithRed:85 green:85 blue:85 alpha:0.5];
    headerLabel.backgroundColor = [UIColor blackColor];
    headerLabel.font = [UIFont systemFontOfSize:14];
    [headerView addSubview:headerLabel];
    headerView.backgroundColor = [UIColor blackColor];
    if (section == 0) {
        headerLabel.text = @"Detail Information";
    } else if (section == 1) {
        headerLabel.text = @"Assigned Collector";
    }
    return headerView;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end
